package com.dragon.client.controller.auth;

import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.IOperationAutoService;
import com.dragon.repository.domain.dto.OperationAutoDTO;
import com.dragon.repository.domain.vo.OperationAutoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-03
 */
@RestController
@RequestMapping("operationAuto")
@Api(tags = "系统参数")
public class OperationAutoController {

    @Autowired
    IOperationAutoService service;

    @PostMapping("add")
    @ApiOperation("新增")
    public ResponseData addAuto(@RequestBody @Validated(Insert.class)OperationAutoVO vo){
        return ResponseData.success(service.addAuto(OperationAutoVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation("更新")
    public ResponseData updateAuto(@RequestBody @Validated(Update.class) OperationAutoVO vo ){
        service.updateAuto(OperationAutoVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public ResponseData deleteAuto( @RequestParam
                                        @ApiParam(name = "id",value = "id",required = true, type = "int") Integer id){
        service.deleteAuto(id);
        return ResponseData.success();
    }

    @GetMapping("one")
    @ApiOperation("根据参数编码对应参数")
    public ResponseData autoListByCode(
       @ApiParam(name = "systemCode",required = true,type = "string",value = "参数编码")
       @RequestParam String systemCode
    ){
        return ResponseData.success(service.selectDetailBySystemCode(systemCode));
    }


    @GetMapping("getJson")
    @ApiOperation("获取form表单数据")
    public ResponseData getJson(
            @ApiParam(name = "codeType",required = true,type = "int",value = "参数类别")
            @RequestParam Integer codeType,
            @ApiParam(hidden = true)HttpServletRequest request
            ){
        return ResponseData.success(service.selectDetailByCode(codeType,request));
    }


    @PostMapping("editOne")
    @ApiOperation("修改系统配置保存配置")
    public ResponseData updateOne(@RequestBody HashMap<String,String> hashMap){
        service.updateOne(hashMap);
        return ResponseData.success();
    }

    @GetMapping("list")
    @ApiOperation(value = "获取所有配置项")
    public ResponseData selectAllSystem(){
        return ResponseData.success(OperationAutoDTO.convertList(service.selectAllSystem()));
    }

}
